/**
 * @Author your name
 * @Date 2021-03-04 16:58:21
 * @LastEditTime 2021-03-04 17:05:14
 * @LastEditors Please set LastEditors
 * @Description In User Settings Edit
 * @FilePath \fed-e-task-04-06\code\angular-demo\src\app\auth.interceptor.ts
 */
import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { retry } from 'rxjs/operators';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  constructor() { }
  // 拦截方法
  intercept(
    // unknown 指定请求体 (body) 的类型
    request: HttpRequest<unknown>, 
    next: HttpHandler
    // unknown 指定响应内容 (body) 的类型
  ): Observable<HttpEvent<unknown>> {
    // 克隆并修改请求头
    let req = request.clone({
      // method: 'post',
      setHeaders: {
        hello: 'hello'
      }
    })
    // 通过回调函数将修改后的请求头回传给应用 
    // retry 请求失败重新请求
    return next.handle(req).pipe(retry(2))
  }
}
